package com.zzyl.controller;

import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * 添加新的资源
     * @param resourceDto
     * @return
     */
    @PutMapping
    public ResponseResult addResource(@RequestBody ResourceDto resourceDto) {
    resourceService.addResource(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 查询资源列表
     * @param resourceDto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult<List<ResourceVo>>list(@RequestBody ResourceDto resourceDto){
        List<ResourceVo> resourceVoList =resourceService.list(resourceDto);
        return ResponseResult.success(resourceVoList);
    }

    /**
     * 资源树形
     * @param resourceDto
     * @return
     */
    @PostMapping("/tree")
    public ResponseResult<TreeVo>tree(@RequestBody ResourceDto resourceDto){
        TreeVo treeVo =resourceService.tree(resourceDto);
        return ResponseResult.success(treeVo);
    }

    /**
     * 修改资源
     * @param resourceDto
     * @return
     */
    @PatchMapping
    public ResponseResult updateResource(@RequestBody ResourceDto resourceDto) {
        resourceService.updateResource(resourceDto);
        return ResponseResult.success();
    }
    @PostMapping("/enable")
    public ResponseResult enableOrDisable(@RequestBody ResourceDto resourceDto) {
        resourceService.enableOrDisable(resourceDto);
        return ResponseResult.success();
    }
    @DeleteMapping("/{resourceNo}")
    public ResponseResult delete(@PathVariable String resourceNo){
        resourceService.delete(resourceNo);
        return ResponseResult.success();
    }
}
